import { defineStore } from 'pinia';
import { ref } from 'vue';
import { getRegisterAccount, getphoneregCode } from '@/apis/index';
import { loginData } from '@/types/index';
import { showConfirmDialog, showDialog, showToast } from 'vant';
import { getemailregCode, getemail } from '@/apis/index';
import { LocationQuery } from 'vue-router';
import router from '@/router';
import { setAccessToken, setRefershToken } from '@/config/storage';
export const useUserLogin = defineStore('login', () => {
    // 登录账号密码
    const username = ref({
        user: '',
        pass: ''
    });
    // 请记住选项
    const checked = ref(false);
    const code = ref();//储存验证码返回值
    const color = ref("red");//获取验证码颜色
    const codeItem = ref(false); //验证码隐藏
    const time = ref(6000);//验证码倒计时
    const values = ref('');//验证码值
    const myphone = ref('')//手机号
    const flag=ref(true)//注册还是修改开关
    // 验证码信息注册
    const mailbox = ref({
        email: '',
        username: '',
        img: '',
        phone: ''
    })
    //注册验证
    const getStoreregisterAccount = async (val: LocationQuery) => {
        let res = await getRegisterAccount(val) as loginData
        if (res.code === 200) {
            mailbox.value.email = res.data.email
            mailbox.value.username = res.data.username
            mailbox.value.phone = res.data.phone
            mailbox.value.img = res.data.avatar?res.data.avatar:'http://localhost:3001/image/headimg/head (1).png'
            showDialog({
                message: res.msg,
            }).then(() => {
                router.push('/setpass');
                flag.value=true;
            });
        } else {
            showToast(res.msg);
        }
    }
    // 获取手机验证码
    const getStorephoneregCode = async (phone: LocationQuery) => {
        let res = await getphoneregCode(phone) as loginData;
        console.log(res);
        if (res.code == 200) {
            code.value = res.data.randomCode
            setAccessToken(res.data.accessToken)
            setRefershToken(res.data.refreshToken)
            mailbox.value={
                email: '',
                username: '',
                img: '',
                phone: ''
            }
        } else {
            showConfirmDialog({ message: '手机号不存在，是否去注册？', })
                .then(() => {
                    router.push('/register')
                })
                .catch(() => {
                    router.push('/phonecode')
                });
        }
    };
    // 邮箱验证
    const getStoreemail = async (email: LocationQuery) => {
        let res = await getemail(email) as loginData
        if (res.code == 200) {
            router.push('/authcode')
            mailbox.value.email = res.data.email
            mailbox.value.username = res.data.username
            mailbox.value.img = res.data.avatar
        } else {
            showConfirmDialog({ message: '邮箱不存在，是否去注册？', })
                .then(() => {
                    router.push('/register')
                })
                .catch(() => {
                    router.push('/emailpass')
                });
        }
    }
    //获取邮箱验证码
    let getStoreemailregCode = async () => {
        let res = await getemailregCode({ email: mailbox.value.email }) as loginData
        console.log(res);
        if (res.code == 200) code.value = res.data.randomCode
    };
    // 修改倒计时
    const setTime = (num: number) => time.value = num;
    // 清空账号密码
    const setUsername = () => {
        if (!checked.value) {
            username.value.user = ''
            username.value.pass = ''
        }
    };
    const setflag=(val:any)=>{
        flag.value=val
    }
    // 修改头像
    const setuploadImg=(img:string)=>{
        mailbox.value.img=`http://localhost:3001/image/headimg/${img}`
        console.log(mailbox.value.img);
    }
    return { code, getStorephoneregCode,setuploadImg, getStoreregisterAccount,flag,setflag, color, codeItem, time, values, setTime, getStoreemailregCode, username, setUsername, myphone, mailbox, checked, getStoreemail }
}, {
    persist: {
        enabled: true, // 开启数据缓存
        strategies: [
            {
                key: 'mylogin',
                storage: localStorage // 默认是sessionStorage
            }
        ],
    }
})